/*
 * Copyright 2008 Nathan C Jones
 * 
 * This file is part of Fivehundred.
 * 
 * Fivehundred is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Fivehundred is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * Fivehundred. If not, see <http://www.gnu.org/licenses/>. 
 */

package com.googlecode.fivehundred.model;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.googlecode.fivehundred.svg.DefaultCardBackRenderer;

/**
 * Default deck implementation.
 * 
 * @author Nathan C Jones
 */
class DefaultDeck implements Deck {

	/** Mapping from cards to card rank. */
	private Map<Card, Integer> cards;

	/**
	 * Create a new deck with the given cards. The card ranks will be determined
	 * by the ordering of the cards with the first card having the highest rank.
	 * Duplicate cards (according to the equals contract) will be completely
	 * ignored; they will not be added to the deck and they will not affect the
	 * ranking of the original card. This means that only the first occurance of
	 * a card will govern the ranking.
	 * 
	 * @param cards the cards to be available in the deck.
	 * @throws IllegalArgumentException if any card is <code>null</code>.
	 */
	public DefaultDeck(Card... cards) {
		this.cards = new HashMap<Card, Integer>();
		int rank = 1;
		for (int i = 0; i < cards.length; i++) {
			if (cards[i] == null) {
				throw new IllegalArgumentException("Cannot create a deck with a null card.");
			}
			if (!this.cards.containsKey(cards[i])) {
				this.cards.put(cards[i], rank++);
			}
		}
	}

	public Integer getRank(Card card) {
		return cards.get(card);
	}

	public Integer getSize() {
		return cards.size();
	}

	public Iterator<Card> iterator() {
		return Collections.unmodifiableCollection(cards.keySet()).iterator();
	}

	public CardRenderer getCardBackRenderer() {
		return new DefaultCardBackRenderer();
	}
}
